package com.chance.cc.crawler.development.bootstrap.tianmao;

import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @author bx
 * @date 2020/11/18 0018 18:13
 */
public class TianmaoSearchHtmlStart {

//    private static final String searchUrl = "https://s.taobao.com/search?ajax=true&callback=jsonp764&q=%s" +
//            "&ie=utf8&js=1&stats_click=search_radio_all%%3A1&p4ppushleft=1%%2C48";
    private static final String searchUrl = "https://s.taobao.com/search";

    public static final String domainId = "tianmao";

    public static void main(String[] args) {

        FilterInfo countFilterInfo = new FilterInfo();
        countFilterInfo.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        countFilterInfo.setCountTotalNum(100);
        countFilterInfo.setCurCount(0);

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPage)
                .httpUrl(searchUrl)
                .httpHead("referer","https://s.taobao.com/")
                .httpHead("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
                .httpHead("cookie","_samesite_flag_=true; cookie2=1b657d46efd4104d1dfe089dc4a1835c; t=6952f998ef3b907ad51292a7cbf8cb4d; _tb_token_=335abab33b9ea; cna=EQk9GPo4tywCATohFCIv0irN; xlly_s=1; lgc=%5Cu7CFB%5Cu8FD8%5Cu7B97%5Cu4E0D; dnk=%5Cu7CFB%5Cu8FD8%5Cu7B97%5Cu4E0D; tracknick=%5Cu7CFB%5Cu8FD8%5Cu7B97%5Cu4E0D; _cc_=UIHiLt3xSw%3D%3D; sgcookie=E1002dnfUSdAdGIP6yADLAzWStxjhLkgB0F12nqltpl1wQb5WvmUK9C%2FBqBYFnfvuCpaeJb9e59jRdwPZWyrAKQu7A%3D%3D; uc3=nk2=rL9CGfhTA0w%3D&lg2=UIHiLt3xD8xYTw%3D%3D&vt3=F8dCufwsARmN2xd%2B3hg%3D&id2=UUkJYAV8aFQ8rg%3D%3D; csg=72fcf25b; skt=feb5b85bc8e9665d; existShop=MTYwNTc3MDE4Mw%3D%3D; uc4=nk4=0%40rpHMnVi0ocDUvwAd92lMKLQEmg%3D%3D&id4=0%40U2uFVlzHXCqDYyRTGjsxkgJMFzGo; mt=ci=0_1; thw=cn; v=0; _m_h5_tk=77c878ea553015b36a760c4109aab104_1605777392010; _m_h5_tk_enc=0239c12254ea1dfbcf9f98972b119114; enc=oIjV9uj7Tq8%2B9NhSiPMLibYaG9mOrHlUetl6NApTTB9pnppJIqD6ED3KAGsPPEdkdbBl9nFihtJJowHXgm7n4w%3D%3D; alitrackid=www.taobao.com; lastalitrackid=www.taobao.com; hng=CN%7Czh-CN%7CCNY%7C156; x5sec=7b227365617263686170703b32223a226266353434363536356131363639623737396332343132353064653937643437434f336132503046454a62653959727171764f564f526f4d4d6a45334e6a41304e7a41304f447378227d; uc1=cookie21=UIHiLt3xThH8t7YQp6aL&cookie14=Uoe0aDvbLb%2FDQQ%3D%3D&cookie16=U%2BGCWk%2F74Mx5tgzv3dWpnhjPaQ%3D%3D&pas=0&existShop=false; JSESSIONID=E9FB2FED57291EFF4ADD0698C29D767C; isg=BNvb7dB4ANT3MXygptabS1kxaj9FsO-ytnpZP80YVVqDrPmOVIOZA_BqRgwii0eq; tfstk=cMrOB0cyR6fi7OHLUPQHc7f8HZZOaUQti6cyDT_c85gMR1tvcsc8qudScHDJ6DBd.; l=eBOxt66gO3-sjI3vBO5Cnurza779fIRbz1PzaNbMiInca69cTnae3NQVzOF97dtjgt5cLeKr09hQ7RH67k438xgKqelyRs5mpZJ68e1..")
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(domainId))
                .turnPageFilterInfo(countFilterInfo)
                .needWashed(false)
                .needParsed(false)
                .build();
        requestRecord.setDownload(false);

        String resultQueueName = "tianmao_search_result";
        CrawlerQueueConfig crawlerQueueConfig = new CrawlerQueueConfig(
                resultQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);

        //关键词 source record
        CrawlerRequestRecord keywordCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_keywords",turnPageItem)
                .httpUrl("http://localhost:9599//v1/meta/tianmao/keys")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //商品信息同步到mysql callback record
        CrawlerRequestRecord syncCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_sync",turnPageItem)
                .httpUrl("http://localhost:9599/crawler/domain/common/api/v1/tianmao/search/results/sync?resultQueue="+resultQueueName)
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                .consoleResultPipeline()
//                .queueResultPipeline(crawlerQueueConfig)//redis pipeline result
                .fileResultPipeline("D:\\chance\\log\\tets_tianmao.log",false)
                .requestRecord(requestRecord)
                .supportRecord(keywordCrawlerRecord)
//                .supportRecord(syncCrawlerRecord)
                .build().start();
    }
}
